﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace PayloadTracker
{
    public delegate void OnNewFlightDelegate(Flight flight);
    public partial class NewFlight : Form
    {
        public event OnNewFlightDelegate OnNewFlight;

        APRS.APRSConfiguration m_Config;
        APRS.IAPRSProvider m_Provider;
        NewProvider m_NewProviderDialog;
        public NewFlight(APRS.APRSConfiguration config)
        {
            
            m_Provider = null;
            m_Config = config;
            InitializeComponent();
        }

        void m_NewProviderDialog_OnNewProvider(PayloadTracker.APRS.IAPRSProvider provider)
        {
            m_Config.APRSProviders.Add(provider);
            SetupProviders();
            cbProvider.SelectedItem = provider.Name;
        }
        private void NewFlight_Load(object sender, EventArgs e)
        {
            SetupColors();
            SetupProviders();
        }
       
        void SetupColors()
        {
            List<string> colors = new List<string>();
            foreach(string s in Enum.GetNames(typeof(KnownColor)))
            {
                KnownColor k = (KnownColor)Enum.Parse(typeof(KnownColor),s);
                if(k > KnownColor.Transparent)
                    colors.Add(s);
            }


            cbObject.Items.AddRange(colors.ToArray());
            cbPlot.Items.AddRange(colors.ToArray());


            cbObject.SelectedItem = "Red";
            cbPlot.SelectedItem = "Black";
        }

        void SetupProviders()
        {
            cbProvider.Items.Clear();

            cbProvider.Items.Add("New...");
            foreach(APRS.IAPRSProvider p in m_Config.APRSProviders)
                cbProvider.Items.Add(p.Name);




        }
       

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (m_Provider == null)
            {
                MessageBox.Show("Please select an APRS Provider.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (txtCallsign.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter a callsign to filter.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            OnNewFlightDelegate handler = OnNewFlight;
            if (handler != null)
                handler(new Flight(m_Provider, txtCallsign.Text.Trim().ToUpper(), Color.FromName(cbObject.SelectedItem as string), Color.FromName(cbPlot.SelectedItem as string)));

            this.Close();
        }

        private void cbProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbProvider.SelectedIndex == 0)
            {
                m_NewProviderDialog = new NewProvider();
                m_NewProviderDialog.OnNewProvider += new OnNewProviderDelegate(m_NewProviderDialog_OnNewProvider);
                m_NewProviderDialog.Show(this);
                return;
            }

            m_Provider = m_Config.APRSProviders[cbProvider.SelectedIndex - 1];

        }



    }
}
